package ch.ethz.fcl.metrobuzz.algorithm.voronoi.interpolation;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import ch.ethz.fcl.metrobuzz.MBContainer;
import ch.ethz.fcl.metrobuzz.algorithm.voronoi.AccessibilityVoronoiCell;
import ch.ethz.fcl.metrobuzz.controller.MBController;
import ch.ethz.fcl.metrobuzz.controller.external.ActionEvent;
import ch.ethz.fcl.metrobuzz.data.MBData;
import ch.ethz.fcl.metrobuzz.data.loader.plan.SingaporeTransitPlanFileLoader;
import ch.ethz.fcl.metrobuzz.render.MBRenderer;
import ch.ethz.fcl.metrobuzz.render.techniques.colors.MBColorMap;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Point2D;
import ch.ethz.fcl.metrobuzz.spatialindex.object3d.Point3D;
import ch.ethz.fcl.metrobuzz.tools.MBGL;
import ch.ethz.fcl.metrobuzz.viewer.MBViewer;
import ch.ethz.fcl.metrobuzz.viewer.MBViewer.ProjectionType;

/**
 * 
 * @author ZengWei
 * 
 */
public class InterpolationVoronoiTest {
	private MBContainer container;

	private List<AccessibilityVoronoiCell> cells;

	private float timeLimit = 60 * 60;

	private MBColorMap colorMap;

	public static void main(String[] args) {
		new InterpolationVoronoiTest().run();
	}

	public InterpolationVoronoiTest() {
		init();
	}

	private void run() {
		while (!Display.isCloseRequested()) {
			GL11.glPushMatrix();
			container.getViewer().update();

			container.getControl().update();

			update();
			render();

			GL11.glPopMatrix();

			Display.update();
			Display.sync(32);
		}
		Display.destroy();
		System.exit(0);
	}

	private void init() {
		cells = new ArrayList<AccessibilityVoronoiCell>();

		container = new MBContainer();

		MBViewer view = new MBViewer(ProjectionType.Ortho, container);

		new MBRenderer(container);

		int screenWidth = Display.getDesktopDisplayMode().getWidth();
		int screenHeight = Display.getDesktopDisplayMode().getHeight();

		view.createDisplay(screenWidth, screenHeight, screenWidth,
				screenHeight, "Voronoi test");
		view.getCamera().initDistance(1.0f);
		view.getCamera().enablePan();

		view.initFrame();

		MBData data = new MBData(container);
		new SingaporeTransitPlanFileLoader().load(data,
				"res/Singapore/roadnetwork");
		data.init();

		colorMap = new MBColorMap(null, null, new String[] { "0", "30min",
				"60min" }, new float[] { 0, 30 * 60, 60 * 60 });

		new MBController(container);
	}

	private void update() {
		ActionEvent action = container.getControl().getMouseListener()
				.getRightMouse();

		if (action.getAction() == ActionEvent.ACTION_DOWN) {
			Point3D clickPoint = container
					.getViewer()
					.getProjection()
					.getWorldCoord(
							new Point2D(action.getEventX(), action.getEventY()),
							0);

			float weight = (float) (Math.random() * timeLimit);

			cells.add(new AccessibilityVoronoiCell(clickPoint.getX(),
					clickPoint.getY(), weight));
		}
	}

	private void render() {
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		GL11.glClearColor(1, 1, 1, 1);

		GL11.glEnable(GL11.GL_DEPTH_TEST);

		Random rand = new Random(100);
		MBGL.enableBlend();

		for (int i = 0; i < cells.size(); i++) {
			float r = rand.nextFloat();
			float g = rand.nextFloat();
			float b = rand.nextFloat();
			GL11.glColor4f(r, g, b, 1);
			cells.get(i).render(container, colorMap, timeLimit);
		}

		MBGL.disableBlend();
	}
}
